package dao.impl;

import dao.flowerstoreMapper;
import domain.OrderResult;
import domain.flowerstore;
import utill.JDBCUtil;
import utill.ResultUtil;

import java.sql.*;
import java.util.List;

public class flowerstoreMapperImpl implements flowerstoreMapper {

    /**
     * 通过 商店名称，和商店密码查询是否存在该商店;
     * @param storeName
     * @param storePassword
     * @return
     */
    @Override
    public flowerstore findByUsernameAndPassword(String storeName ,String storePassword) {
        Connection connection=null;
        PreparedStatement ps =null;
        ResultSet rs =null;
        List<flowerstore> fsList=null;
        try{
            connection = JDBCUtil.getConn();
            String sql ="select * from flowerstore where storeName=? and storePassword=?";
            ps=connection.prepareStatement(sql);
            ps.setString(1,storeName);
            ps.setString(2,storePassword);
            rs=ps.executeQuery();
            fsList= ResultUtil.PackingFlowerStore(rs);
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            JDBCUtil.closeAll(ps ,rs,connection);
        }
        return fsList.isEmpty() ? null : fsList.get(0);
    }


    /**
     * 查询所有订单展示数据;
     * @return  返回由OrderResult 包装的数据;
     */
    @Override
    public List<OrderResult> findOrderResult() {
        Connection connection =null;
        Statement st =null;
        ResultSet rs =null;
        List<OrderResult> orderResultList =null;
        try{
            connection=JDBCUtil.getConn();
            st=connection.createStatement();
            String sql="SELECT  c.customerName , o.flowertype ,o.flowernumber,o.totalprice ,o.orderdate from customer  as c JOIN  `order`  as o ";
            rs=st.executeQuery(sql);
            orderResultList=ResultUtil.PackingOrderResult(rs);
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            JDBCUtil.release(connection,st,rs);
        }

        return orderResultList;
    }
}

